#! /usr/bin/env python
# -*- coding: utf-8 -*-

"""
html解析测试 - 节点选择器(tag)
"""

from bs4 import BeautifulSoup

html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>

<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>

<p class="story">...</p>
"""

markup = "<b><!--Hey, buddy. Want to buy a used parser?--></b>"


def main():
    """
    测试
    :return:
    """
    soup = BeautifulSoup(html_doc, "lxml")
    soup1 = BeautifulSoup(markup, "lxml")

    tag_a = soup.a
    print(tag_a)    # tag标签对象
    nav_str = tag_a.string
    print(nav_str)

    comment = soup1.b.string
    print(comment)

    print(type(soup))

    print('*' * 40)
    print(soup.head)    # 获取head标签
    print(soup.p.b) #获取p节点下的b节点
    print(soup.a.string) #获取a标签下的文本，只获取第一个

    print('*' * 40)
    print(soup.body.name)   # 获取节点的名称

    print(soup.p.attrs['class'])    # 获取p节点的class属性

    print('*' * 40)
    print(soup.body.contents)   # 获取节点body下的直接子节点

    print('*' * 40)
    descendants = soup.body.descendants
    for x in descendants:
        print(x)


if __name__ == '__main__':
    main()

